package com.cn.qgx428.orderingmeal;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.annotation.NonNull;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.cn.qgx428.orderingmeal.fragment.CollectionFragment;
import com.cn.qgx428.orderingmeal.fragment.FoodFragment;
import com.cn.qgx428.orderingmeal.fragment.SearchFragment;
import com.cn.qgx428.orderingmeal.myClass.MyKeyInfo;

import java.util.ArrayList;
import java.util.List;

public class Home extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = "HomeActivity";

    private MyKeyInfo keyInfo;

    private AppBarLayout barLayout;
    private Toolbar toolbar;
    private DrawerLayout drawerLayout;
    private NavigationView navView;
    private TextView home_navUserName;

    private FragmentManager fragmentManager;
    private FragmentTransaction fragmentTransaction;
    private FoodFragment foodFragment;
    private CollectionFragment collectionFragment;
    private SearchFragment searchFragment;
    private LinearLayout foodLayout;
    private LinearLayout collectionLayout;
    private LinearLayout searchLayout;
    private ImageView foodBtn;
    private ImageView collectionBtn;
    private ImageView searchBtn;

    private static final int TO_MINE_INFO = 0;
    private static final int TO_CAR = 1;
    private static final int TO_ORDER = 2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        init();
        //设置标题栏
        setTitleColmn();
    }

    private void setTitleColmn() {
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar!=null){
            actionBar.setDisplayHomeAsUpEnabled(true);
            //设置图标按钮提示可引出navigationView
            actionBar.setHomeAsUpIndicator(R.drawable.home_nav_nav);
        }
    }

    private void init() {
        keyInfo = (MyKeyInfo) getIntent().getSerializableExtra("keyInfo");
        Toast.makeText(this, "您好，"+keyInfo.getUserName(), Toast.LENGTH_SHORT).show();
        barLayout = (AppBarLayout)findViewById(R.id.home_appBarLayout);
        toolbar = (Toolbar) findViewById(R.id.home_toolbar);
        drawerLayout = (DrawerLayout) findViewById(R.id.home_drawerLayout);
        navView = (NavigationView) findViewById(R.id.home_navView);
        navView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()){
                    case R.id.home_nav_mine:
                        goToActivity(TO_MINE_INFO);
                        break;
                    case R.id.home_nav_order:
                        goToActivity(TO_ORDER);
                        break;
                    case R.id.home_nav_car:
                        goToActivity(TO_CAR);
                        break;
                    case R.id.home_nav_logout:
                        loOut();
                }
                return true;
            }
        });
        View headerView = navView.getHeaderView(0);
        home_navUserName = (TextView) headerView.findViewById(R.id.home_navUserName);
        home_navUserName.setText(keyInfo.getUserName());

        //设置fragment页面导航按钮事件
        fragmentManager = getSupportFragmentManager();
        foodFragment = new FoodFragment();
        searchFragment = new SearchFragment();
        collectionFragment = new CollectionFragment();
        fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.home_fragmentLayout, foodFragment);
        fragmentTransaction.add(R.id.home_fragmentLayout, searchFragment);
        fragmentTransaction.add(R.id.home_fragmentLayout, collectionFragment);
        fragmentTransaction.show(foodFragment);
        fragmentTransaction.commit();
        foodBtn = (ImageView) findViewById(R.id.home_fragmentNavFoodBtn);
        searchBtn = (ImageView) findViewById(R.id.home_fragmentNavSearchBtn);
        collectionBtn = (ImageView) findViewById(R.id.home_fragmentNavCollectionBtn);
        foodLayout = (LinearLayout)findViewById(R.id.home_fragmentNavFood);
        collectionLayout = (LinearLayout)findViewById(R.id.home_fragmentNavCollection);
        searchLayout = (LinearLayout)findViewById(R.id.home_fragmentNavSearch);
        foodLayout.setOnClickListener(this);
        collectionLayout.setOnClickListener(this);
        searchLayout.setOnClickListener(this);
        selectTag(1);
        changeFragment(foodFragment,1);
    }

    private void goToActivity(int road) {
        Intent intent = null;
        if (road == TO_MINE_INFO){
            intent = new Intent(Home.this,Mine.class);
            intent.putExtra("keyInfo",keyInfo);
            startActivity(intent);
        }else if(road == TO_CAR){
            intent = new Intent(Home.this,CarActivity.class);
            intent.putExtra("keyInfo",keyInfo);
            startActivity(intent);
        }else if (road == TO_ORDER){
            intent = new Intent(Home.this,OrderActicity.class);
            intent.putExtra("keyInfo",keyInfo);
            startActivity(intent);
        }
    }

    private void loOut() {
        SharedPreferences sp = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString("isLogined","0");
        editor.commit();
        startActivity(new Intent(Home.this,Login.class));
        Toast.makeText(this, "已注销", Toast.LENGTH_SHORT).show();
        finish();
    }

    /**
     *设置navigationView事件
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.home_navmenu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            //菜单ID默认为home
            case android.R.id.home:
                drawerLayout.openDrawer(GravityCompat.START);
                break;
        }
        return true;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.home_fragmentNavFood:
                changeFragment(foodFragment , 1);
                break;
            case R.id.home_fragmentNavSearch:
                changeFragment(searchFragment, 2);
                break;
            case R.id.home_fragmentNavCollection:
                changeFragment(collectionFragment, 3);
                break;
        }
    }

    private void changeFragment(Fragment showFragment, int page) {
        fragmentTransaction= fragmentManager.beginTransaction();
        fragmentTransaction.hide(foodFragment);
        fragmentTransaction.hide(searchFragment);
        fragmentTransaction.hide(collectionFragment);
        fragmentTransaction.show(showFragment);
        fragmentTransaction.commit();
        selectTag(page);
    }

    private void selectTag(int page) {
        foodBtn.setImageResource(R.drawable.home_fragment_nav_home);
        searchBtn.setImageResource(R.drawable.home_fragment_nav_search);
        collectionBtn.setImageResource(R.drawable.home_fragment_nav_collection);
        switch (page){
            case 1:
                foodBtn.setImageResource(R.drawable.home_fragment_nav_home1);
                barLayout.setVisibility(View.VISIBLE);
                break;
            case 2:
                searchBtn.setImageResource(R.drawable.home_fragment_nav_search1);
                barLayout.setVisibility(View.INVISIBLE);
                break;
            case 3:
                collectionBtn.setImageResource(R.drawable.home_fragment_nav_collection1);
                barLayout.setVisibility(View.INVISIBLE);
                break;
        }
    }
}
